package com.hengtiansoft.lfy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName: SceneDetailsAdapter
 * @Description: 场景ListView
 * @author caijiajun
 * @date 2015年10月14日 下午5:23:26
 * 
 */
public class SceneDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mList;
    SceneDetailsAdapterListener mListener;
    private int mLastPosition;

    public void setAdapterListener(SceneDetailsAdapterListener listener) {
        this.mListener = listener;
    }

    public SceneDetailsAdapter(Context context,
                               ArrayList<HashMap<String, Object>> list) {
        mContext = context;
        this.mList = list;
        mInflater = LayoutInflater.from(mContext);
        mLastPosition = -1;
    }

    public final class ViewHolder {
        TextView mTvText;
        TextView mTvTranslation;
        ImageButton mBtnPlayerTranslation;
        ImageButton mIbtnCopy;
        ImageButton mIbtnTranspond;
        ImageButton mIbtnCollect;
        ImageButton mIbtnOther;
        ImageButton mIbtnFull;
        ImageButton ibtn_share;
        RelativeLayout mRl;
        LinearLayout mLl;
        View mViewLine;
        ImageView iv_click;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return mList.get(position);
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.activity_scene_details_listview_item, null);
            holder.mTvText = (TextView) convertView.findViewById(R.id.tv_text);
            holder.mTvTranslation = (TextView) convertView
                    .findViewById(R.id.tv_translation);
            holder.mBtnPlayerTranslation = (ImageButton) convertView
                    .findViewById(R.id.ibtn_player_translation);
            holder.mIbtnCopy = (ImageButton) convertView
                    .findViewById(R.id.ibtn_copy);
            holder.mIbtnTranspond = (ImageButton) convertView
                    .findViewById(R.id.ibtn_transpond);
            holder.mIbtnCollect = (ImageButton) convertView
                    .findViewById(R.id.ibtn_collect);
            holder.mIbtnOther = (ImageButton) convertView
                    .findViewById(R.id.ibtn_other);
            holder.mRl = (RelativeLayout) convertView
                    .findViewById(R.id.rl_other);
            holder.mLl = (LinearLayout) convertView
                    .findViewById(R.id.ll_translation);
            holder.mViewLine = (View) convertView.findViewById(R.id.view_line);
            holder.mIbtnFull = (ImageButton) convertView
                    .findViewById(R.id.ibtn_full_screen);
            holder.ibtn_share= (ImageButton) convertView.findViewById(R.id.ibtn_share);
            holder.iv_click= (ImageView) convertView.findViewById(R.id.iv_click);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (mLastPosition == position) {
            //显示和影藏copy列
            if (holder.mRl.getVisibility() == View.GONE) {
                holder.mRl.setVisibility(View.VISIBLE);
            } else {
                holder.mRl.setVisibility(View.GONE);
                mLastPosition = -1;
            }

            holder.iv_click.setImageResource(R.drawable.i_down);
        } else {
            holder.mRl.setVisibility(View.GONE);
            holder.iv_click.setImageResource(R.drawable.i_right);
        }
        holder.mLl.setVisibility(View.VISIBLE);
        holder.mViewLine.setVisibility(View.GONE);
       /* if (mLastPosition == position) {
            if (holder.mRl.getVisibility() == View.GONE
                    & holder.mLl.getVisibility() == View.GONE) {
                holder.mViewLine.setVisibility(View.GONE);
                holder.mRl.setVisibility(View.VISIBLE);
                holder.mLl.setVisibility(View.VISIBLE);
            } else {
                holder.mViewLine.setVisibility(View.VISIBLE);
                holder.mRl.setVisibility(View.GONE);
                holder.mLl.setVisibility(View.GONE);
            }
            mLastPosition = -1;
        } else {
            holder.mViewLine.setVisibility(View.VISIBLE);
            holder.mRl.setVisibility(View.GONE);
            holder.mLl.setVisibility(View.GONE);

        }*/
        final HashMap<String, Object> item = getItem(position);
        holder.mTvText.setText(item.get("text").toString());
        holder.mTvTranslation.setText(item.get("translation").toString());
       /* holder.mTvTranslation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onPlayerTranslationClick(position,v);

            }
        });
        holder.mBtnPlayerTranslation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onPlayerTranslationClick(position,v);

            }
        });*/
        holder.mLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onPlayerTranslationClick(position,v);

             }

        });
       /* holder.mLl.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mListener != null)
                    mListener.onItemClick(position);
                return false;
            }
        });*/
        holder.iv_click.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                if (mListener != null)
                    mListener.onItemClick(position,view);
            }
        });
        holder.mIbtnCopy.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onCopyClick(position);
            }
        });
        holder.mIbtnTranspond.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onTranspondClick(position);
            }
        });

        holder.ibtn_share.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onShareClick(position);
            }
        });
        if(item.containsKey("collected")&&item.get("collected").toString().equals("SS")){
            holder.mIbtnCollect.setVisibility(View.GONE);
        }
        if (item.containsKey("collected")&&item.get("collected").toString().equals("Y")) {
            holder.mIbtnCollect
                    .setBackgroundResource(R.drawable.interpret_iscollect);
        } else {
            holder.mIbtnCollect
                    .setBackgroundResource(R.drawable.interpret_collect);
        }
         holder.mIbtnCollect.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
         if (mListener != null)
         mListener.onCollectClick(position);
         }
         });

        holder.mIbtnOther.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onOtherClick(position);
            }
        });
        holder.mIbtnFull.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onFullClick(position);
            }
        });
     /*   convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mListener != null)
                    mListener.onItemClick(position);
            }
        });*/
        /*convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mListener != null)
                    mListener.onItemClick(position);
                return false;
            }
        });*/
        return convertView;
    }

    // 显示和隐藏item下面的按钮
    public void changeImageVisable(int position) {
        mLastPosition = position;
    }

    public interface SceneDetailsAdapterListener {

        public void onPlayerTranslationClick(int position, View v);

        public void onCopyClick(int position);

        public void onTranspondClick(int position);

        public void onCollectClick(int position);

        public void onOtherClick(int position);

        public void onFullClick(int position);

        public void onItemClick(int position,View view);//

        public void onShareClick(int position);
    }
}
